package com.gzist.elderlyserviceplatform.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gzist.elderlyserviceplatform.common.Result;
import com.gzist.elderlyserviceplatform.entity.dto.InfoNamePageDTO;
import com.gzist.elderlyserviceplatform.entity.po.InfoCategory;
import com.gzist.elderlyserviceplatform.service.InfoCategoryService;
import com.gzist.elderlyserviceplatform.service.InfoNewsService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * 资讯分类表
 */
@RestController
@Tag(name = "资讯分类管理")
@RequestMapping("/api/admin/infoCategory")
@RequiredArgsConstructor
public class InfoCategoryControllerA {

    private final InfoCategoryService infoCategoryService;
    private final InfoNewsService infoNewsService;

    @PostMapping("/add")
    @Operation(summary = "资讯分类添加", description = "InfoCategory")
    public Result<InfoCategory> addInfoCategory(
            @RequestBody
            @Parameter(description = "资讯分类信息") InfoCategory infoCategory
    ) {
        infoCategoryService.save(infoCategory);
        return Result.success("咨询分类信息添加成功");
    }

    @PostMapping("/list")
    @Operation(summary = "资讯分类列表获取", description = "分类过滤查询资讯分类列表，可根据分类名称进行模糊查询")
    public Result<Page<InfoCategory>> pageInfoCategory(
            @RequestBody
            @Parameter(description = "查询参数") InfoNamePageDTO infoNamePageDTO
    ) {
        return Result.success(infoCategoryService.queryPage(infoNamePageDTO));
    }

    @PostMapping("/update")
    @Operation(summary = "资讯分类修改", description = "InfoCategory")
    public Result<InfoCategory> updateInfoCategory(
            @RequestBody
            @Parameter(description = "资讯分类信息") InfoCategory infoCategory
    ) {
        infoCategoryService.updateById(infoCategory);
        return Result.success("修改成功！");
    }

    @GetMapping("/detail/{id}")
    @Operation(summary = "资讯分类查看详情", description = "InfoCategory")
    public Result<InfoCategory> getInfoCategoryById(
            @PathVariable("id")
            @Parameter(description = "资讯分类id") String id
    ) {
        return Result.success(infoCategoryService.getById(id), "查询成功!");
    }

    @PostMapping("/delete/{id}")
    @Transactional(rollbackFor = Exception.class)
    @Operation(summary = "资讯分类删除", description = "InfoCategory")
    public Result deleteInfoCategoryById(
            @PathVariable("id")
            @Parameter(description = "资讯分类id") String id
    ) {
        infoCategoryService.removeById(id);
        infoNewsService.c_delete(id);
        return Result.success("删除成功!");
    }
}